import { message, Upload } from 'antd';
import PropTypes from 'prop-types';
import React from 'react';

class UploadFile extends React.Component {
  state = {
    fileList: this.props.fileList,
    uploadUrl: this.props.uploadUrl,
  };
  UNSAFE_componentWillReceiveProps(nextProps) {
    if (nextProps.fileList !== this.props.fileList) {
      this.setState({
        fileList: nextProps.fileList,
      });
    }
  }
  handleChange = (info) => {
    const { onUploadSuccess, onUploading, onProcess, onUploadFail, onRemoveSuccess } = this.props;
    const { file, fileList } = info;
    switch (file.status) {
      case 'uploading':
        onUploading && onUploading(file);
        onProcess && info.event && onProcess(info.event.percent, file);
        break;
      case 'done':
        onUploadSuccess(file.response.data);
        break;
      case 'error':
        onUploadFail ? onUploadFail(file) : message.error('上传失败！！', 1);
        break;
      case 'removed':
        onRemoveSuccess ? onRemoveSuccess(file.uid) : message.success('移除成功！！', 1);
        break;
      default:
        '';
    }

    // 限制上传数量
    if (this.props.limitNum && fileList.length > this.props.limitNum) {
      fileList.shift(1);
    }
    this.setState({ fileList });
  };

  beforeUpload = (file, fileList) => {
    if (!this.props.onBeforeUpload(file, fileList)) {
      return false;
    }
    return true;
  };

  render() {
    return (
      <Upload
        {...this.props}
        name="file"
        action={`${this.props.uploadUrl}`}
        beforeUpload={this.beforeUpload}
        onChange={this.handleChange}
        // data={this.state.uploadParams}
        showUploadList={this.props.show}
        defaultFileList={this.state.fileList}
        fileList={this.state.fileList}
      />
    );
  }
}

UploadFile.propTypes = {
  // 文件上传地址
  uploadUrl: PropTypes.string,
  /**
   * 文件上传前校验 该函数必须有返回值  true或者false!!!!!!!!!!!!!
   * @param {Object} file 文件信息
   * @param {array} fileList 文件列表
   * @return {boolean}
   */
  onBeforeUpload: PropTypes.func,
  /**
   * 上传进度
   * @param {string} process 进度
   * @param {Object} file 文件信息
   */
  onProcess: PropTypes.func,
  /**
   * 上传中
   * @param {Object} info 文件当前状态信息
   */
  onUploading: PropTypes.func,
  /**
   * 上传文件成功时的回调，
   * @param {string} fileUrls 文件地址
   * @param {string} fileName 文件名
   * @param {string} fileType 文件类型
   * @param {Object} file 文件信息
   * @param {array} fileList 文件列表
   */
  onUploadSuccess: PropTypes.func,
  /**
   * 上传文件失败时的回调，
   * @param {Object} file 文件信息
   */
  onUploadFail: PropTypes.func,
  /**
   * 删除文件成功时的回调，
   * @param {string} uid 文件唯一id
   */
  onRemoveSuccess: PropTypes.func,
  /** 功能模块名称 */
  moduleName: PropTypes.string,
  /** 是否展示文件列表 */
  show: PropTypes.bool,
  /** 限制文件上传个数 */
  limitNum: PropTypes.number,
  /** 文件列表 */
  fileList: PropTypes.array,
};

UploadFile.defaultProps = {
  onBeforeUpload: () => {
    return true;
  },
  onUploadSuccess: () => {},
  // moduleName: FBD_IMAGES,
  show: false,
  fileList: [],
};

export default UploadFile;
